<?php
namespace app\common\loglic;

use app\common\loglic\Model;

class Score extends Model
{
    //用户充值积分
    public function incRecharge($userId=0,$totalFree=0,$payId=0)
    {
        if(!$userId || !$totalFree){
            return 0;
        }
        //充值金额*充值比例
        $score = intval($totalFree*config('score.recharge'));
        //积分操作与记录日志
        if(model('common/User')->where('user_id',$userId)->setInc('user_score', $score)){
            return model('common/Log','loglic')->scoreRecharge($userId, $score, $payId);
        }
        return 0;
    }

    //推广赠送积分
    public function incReward($userPid=0)
    {
        if(!$userPid || !config('score.reward')){
            return 0;
        }
        if(model('common/User')->where('user_id',$userPid)->setInc('user_score', config('score.reward'))){
            return model('common/Log','loglic')->scoreReward($userPid, config('score.reward'));
        }
        return 0;
    }

    //注册赠送积分
    public function incRegister($userId=0)
    {
        if(!$userId || !config('score.register')){
            return 0;
        }
        if(model('common/User')->where('user_id',$userId)->setInc('user_score', config('score.register'))){
            return model('common/Log','loglic')->scoreRegister($userId, config('score.register'));
        }
        return 0;
    }

    //应用扣除积分
    public function decApps($userId=0, $appId=0, $score=0, $name='douyin')
    {
        if($result = model('common/User')->where('user_id',$userId)->setDec('user_score', $score)){
            return model('common/Log','loglic')->scoreApps($userId, $appId, $score, $name, 'dec');
        }
        return 0;
    }

    //消费验证
    public function validateConfig($userScore=0, $aiScore=0)
    {
        if($aiScore < 1){
            return true;
        }
        if($userScore < $aiScore){
            $this->error = '积分不足，请充值！';
            return false;
        }
        return true;
    }
}